package com.zf.shuashua.ui.mine;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Build;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.zf.shuashua.R;
import com.zf.shuashua.ShuaApplication;
import com.zf.shuashua.advert.CommitUtils;
import com.zf.shuashua.api.Store;
import com.zf.shuashua.base.BaseActivity1;
import com.zf.shuashua.ui.gift.H5Activity;
import com.zf.shuashua.utils.Util;

public class MineH5Activity extends BaseActivity1 {
    private WebView webview;
    private Toolbar mToolbar;
    private TextView mTvTitle;
    private String url;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mine_h5;
    }

    @Override
    protected void initView() {
        webview = findViewById(R.id.web);
        mToolbar = findViewById(R.id.toolbar);
        mTvTitle = findViewById(R.id.tv_title);
        initToolBar(mToolbar, true, "");
        mTvTitle.setText(getIntent().getStringExtra("title"));
        url = getIntent().getStringExtra("money");
        initWebView();
        loadUrl();
    }

    public void loadUrl() {
        if (webview != null && !TextUtils.isEmpty(url)) {
            webview.loadUrl(url);
        }
    }

    @SuppressLint({"SetJavaScriptEnabled", "AddJavascriptInterface"})
    private void initWebView() {
        WebSettings ws = webview.getSettings();
        // 网页内容的宽度是否可大于WebView控件的宽度
        ws.setLoadWithOverviewMode(false);
        // 保存表单数据
        ws.setSaveFormData(true);
        // 是否应该支持使用其屏幕缩放控件和手势缩放
        ws.setSupportZoom(true);
        ws.setBuiltInZoomControls(true);
        ws.setDisplayZoomControls(false);
        // 启动应用缓存
        ws.setAppCacheEnabled(true);
        // 设置缓存模式
        ws.setCacheMode(WebSettings.LOAD_DEFAULT);
        // setDefaultZoom  api19被弃用
        // 设置此属性，可任意比例缩放。
        ws.setUseWideViewPort(true);
        // 不缩放
        webview.setInitialScale(100);
        // 告诉WebView启用JavaScript执行。默认的是false。
        ws.setJavaScriptEnabled(true);
        //  页面加载好以后，再放开图片
        ws.setBlockNetworkImage(false);
        // 使用localStorage则必须打开
        ws.setDomStorageEnabled(true);
        // 排版适应屏幕
        ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        // WebView是否新窗口打开(加了后可能打不开网页)
//        ws.setSupportMultipleWindows(true);

        // webview从5.0开始默认不允许混合模式,https中不能加载http资源,需要设置开启。
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ws.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        /** 设置字体默认缩放大小(改变网页字体大小,setTextSize  api14被弃用)*/
        ws.setTextZoom(100);

//        mWebChromeClient = new MyWebChromeClient(this);
//        webview.setWebChromeClient(mWebChromeClient);
        // 与js交互
        webview.addJavascriptInterface(MineH5Activity.this, "android");
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
            }

            @Override
            public void onPageFinished(WebView view, String url) {
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });

    }

    @JavascriptInterface
    public String getToken() {
        return ShuaApplication.token;
    }

    @JavascriptInterface
    public String getDeviceId() {
        return Util.getDeviceId(ShuaApplication.getContext());
    }

    @JavascriptInterface
    public String getBlackBox() {
        return ShuaApplication.blackbox;
    }

    @JavascriptInterface
    public String getAccessToken() {
        return ShuaApplication.accessToken;
    }

    @JavascriptInterface
    public void logout() {
        Store.getInstance().logoutFromH5Page(this);
        finish();
    }

    @JavascriptInterface
    public String getAppVersion() {
        return getVersionName();
    }

    @JavascriptInterface
    public String getSystemModel() {
        return android.os.Build.MODEL;
    }

    @JavascriptInterface
    public String getApiVersion() {
        return CommitUtils.API_VERSION;
    }

    @JavascriptInterface
    public String getChannel() {
        return ShuaApplication.channel;
    }

    @JavascriptInterface
    public void login() {
        Intent intent = new Intent(MineH5Activity.this, LoginActivity.class);
        startActivity(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (webview != null) {
            webview.destroy();
        }
    }
}
